<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="filterelementName"
          required
          label="滤芯名称"
        >
          <a-input placeholder="请输入滤芯名称" v-model="model.filterelementName" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="validity" required label="可用天数">
          <a-input placeholder="请输入可用天数" v-model="model.validity" />
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="replacementdays"
          required
          label="最低更换天数"
        >
          <a-input placeholder="请输入最低更换天数" v-model="model.replacementdays" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" required label="濾芯图片">
          <j-image-upload class="avatar-uploader" text="上传" v-model="model.images"></j-image-upload>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addFilterelement, updFilterelement } from '@/api/api'
import JDictSelectTag from '@/components/dict/JDictSelectTag'

let validatorCodeTimer = null

export default {
  name: 'SysPositionModal',
  components: { JDictSelectTag },
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        filterelementName: [{ required: true, message: '请输入滤芯名称' }],
        replacementdays: [{ required: true, message: '请输入最低更换天数' }],
        validity: [{ required: true, message: '请输入可用天数' }],
      },
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    //关闭当前对话框
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.resetFields()
    },
    handleOk() {
      const that = this
      // 触发表单验证 valid:表单的正则验证是否通过
      this.$refs.form.validate((valid) => {
        if (valid) {
          //confirmLoading:加载动画 点击的时候触发,为了防止重复提交
          that.confirmLoading = true
          var object
          if (!this.model.id) {
            object = addFilterelement(this.model)
          } else {
            object = updFilterelement(this.model)
          }

          object
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>

<style lang="less" scoped>
</style>
